<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Quan extends \Phpcmf\Home\Module
{

	public function index() {
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$quan_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $id)->where('fstatus', 1)->get()->getRowArray();
		$quan_data_0 = \Phpcmf\Service::M()->db->table('1_quan_data_0')->where('id', $id)->get()->getRowArray();
		// 加入圈子
		$jiaquan_data = \Phpcmf\Service::M()->table('1_quan_jiaquan')->where('quanid', $id)->getAll();
		// 循环 $jiaquan_data ， 把 $jiaquan_data['uid'] 替换为 用户名
		$touxiang = [];
		foreach($jiaquan_data as $key => $val){
			//判断 小于20 之内的数据
			if($key < 20){
				$touxiang[$key] = dr_avatar($val['uid']);
				
			}
		}
		$user = dr_member_info($quan_data['uid']);
				$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $quan_data['uid'])->get()->getRowArray();
				if($userweixin){
					$mingcheng = $userweixin['nickname'];
				} else if($user['name']) {
					$mingcheng = $user['name'];
				} else {
					$mingcheng = $user['username'];
				}
		// 判断是否购买过 cid 是圈子id
		$yigou_data =  \Phpcmf\Service::M()->db->table('1_quan_buy')->where('uid', $this->uid)->where('cid', $id)->countAllResults();
		if($yigou_data){
			$yigou = 1;
		} else {
			$yigou = 0;
		}
		//计算 运营天数
		$tianshu_0 = ( time() - $quan_data['inputtime']) / 86400;
		//获取$tianshu_0 的整数部分
		$tianshu = intval($tianshu_0);
		//贴数
		$tieshu = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('cid', $id)->where('status', 1)->countAllResults();
		$jieshu_shijian =SYS_TIME + (int)$quan_data['tianshu'] * 24 * 60 * 60;
		// 内容
		$list_data[] = [
			'id' => $quan_data['id'],
			'uid' => $quan_data['uid'],
			'name' => $mingcheng,
			'tubiao' => dr_get_file($quan_data['thumb']),
			'touxiang' => $touxiang,
			'title' => $quan_data['title'],
			'jianjie' => dr_strcut(dr_clearhtml($quan_data['description']), 100),
			'fabushijian' => dr_date($quan_data['inputtime'], 'Y-m-d'),
			'kaifangfabu' => $quan_data['kaifangfabu'],
			'fstatus' => $quan_data['fstatus'],
			'jiage' => $quan_data['jiage'],
			'yigou' => $yigou,
			'tianshu' => $tianshu, //运营天数
			'tieshu' => $tieshu,
			'comments' => $quan_data['comments'],
			'huiyuan_tianshu' => $quan_data['tianshu'],
			'kaishishijian' => dr_date(SYS_TIME, 'Y-m-d'), // 开始时间
			'jieshushijian' => dr_date($jieshu_shijian, 'Y-m-d'), 
			'neirong_data' => htmlspecialchars_decode($quan_data_0['content']),
		];

		$this->_json('1', '读取成功', $list_data);
	}
	public function xuan_quan() {
		$quan_data = \Phpcmf\Service::M()->table('1_quan')->where('fstatus', 1)->getAll();
		$daohang_list_data = \Phpcmf\Service::M()->table('1_quan_category')->where('show', 1)->where('pid', 0)->getAll();
		// 循环 $daohang_list_data ， 把 $daohang_list_data['id'] 作为 1_quan_category 的 pid ， 再查询 1_quan_category 数据
		$daohang_data = [];
		foreach($daohang_list_data as $key => $val){
			$daohang_data[$key]['index'] = $key;
			$daohang_data[$key]['id'] = $val['id'];
			$daohang_data[$key]['name'] = $val['name'];
			
		}

		// 循环 $daohang_data ， 把 $daohang_data['id'] 作为 1_quan_category 的 pid ， 再查询 1_quan_category 数据
		
		$quan_list = [];
		foreach($daohang_list_data as $key => $val){
			// 循环 $val['childids'] ， 把 $val['childids'] 作为 1_quan 的 id ， 再查询 1_quan 数据
			// $val['childids'] 是 1,2,3,4,5,6,7,8,9,10
			$childids = explode(',', $val['childids']);
			// 循环 $val['childids'] ， 把 $val['childids'] 作为 1_quan 的 id ， 再查询 1_quan 数据
			foreach($childids as $k => $v){
				$quan_data = \Phpcmf\Service::M()->table('1_quan')->where('fstatus', 1)->where('catid', $v)->getAll();
				// 循环 $quan_data 
				foreach($quan_data as $keys => $vals){
					$index = count($quan_list);
					$quan_list[$index]['id'] = $vals['id'];
					$quan_list[$index]['type'] = $val['id'];
					$quan_list[$index]['title'] = $vals['title'];
					$quan_list[$index]['img'] = dr_thumb($vals['thumb'], 250, 250, 0, 'crop');
					$quan_list[$index]['desc'] = dr_strcut(dr_clearhtml($vals['description']), 100);
				}
				
			}
			
			
		}
		$data[] = [
			'daohang_list' => $daohang_data,
			'quan_list' => $quan_list,
		];
		$this->_json('1', '读取成功', $data);

	}

}
